package com.xy.novelweb.controller;

import com.xy.novelweb.domain.Author;
import com.xy.novelweb.domain.Msg;
import com.xy.novelweb.domain.Reader;
import com.xy.novelweb.service.LoginAndRegistService;
import com.xy.novelweb.utils.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("loginAndRegist")
@Controller
@Slf4j
public class LoginAndRegistController {
		@Autowired
		private LoginAndRegistService loginAndRegistService;
		private String referUrl = "/";

		@RequestMapping("login")
		public String Login(Model model, HttpSession session, HttpServletRequest request) {
				session.removeAttribute("user_reader");
				session.removeAttribute("user_auth");
//				String urls[] = request.getHeader("Referer").split("wfRead");

				referUrl = "";//urls[1].replaceFirst("/", "");
				log.info(referUrl);
				return "login";
		}

		@RequestMapping("loginout")
		public String LoginOut(Model model, HttpSession session, HttpServletRequest request) {

				Jedis jedis = JedisUtil.getConnect();
				Reader reader = (Reader) session.getAttribute("user_reader");
				if (reader != null) {
						jedis.del("reader:" + reader.getNickName());
				}
				Author author = (Author) session.getAttribute("user_auth");
				if (author != null) {
						jedis.del("auth:" + author.getNickName());
				}

				JedisUtil.close(jedis);

				session.removeAttribute("user_reader");
				session.removeAttribute("user_auth");

				String url = request.getHeader("Referer").replace("http://localhost:8080/wfRead/", "");
				//url = url.replace(url.substring(0,url.lastIndexOf("wfRead/")),"").replace("wfRead/","");
				log.info("LoginOut url=" + url);
				return "redirect:/" + url;
		}

		@RequestMapping("backindexpage")
		public String BackIndexPage() {
				return "redirect:/index";
		}

		@RequestMapping("regist")
		public String Regist(Model model, HttpSession session) {
				model.addAttribute("user_auth", new Author());
				model.addAttribute("user_reader", new Reader());
				return "regist";
		}

		@ResponseBody
		@RequestMapping("authlogin")
		public Msg AuthLogin(Author author, HttpServletRequest request, HttpSession session) {
				Msg msg = new Msg(true);
				if (loginAndRegistService.AuthLogin(author)) {
						Author authorLogin = loginAndRegistService.getAuthMsgByName(author.getNickName());
						msg.setFlag(true);
						msg.setMessage("登录成功！");
						session.setAttribute("user_auth", authorLogin);
						msg.setMessage(referUrl);
				} else {
						msg.setFlag(false);
						msg.setMessage("用户名或密码错误！请检查后重试！");
						session.setAttribute("user_auth", null);
				}
				log.info("authlogin:" + msg);
				return msg;
		}

		@ResponseBody
		@RequestMapping("readerlogin")
		public Msg Readerlogin(Reader reader, HttpSession session) {
				Msg msg = new Msg(true);
				log.info("[login: reader]=" + reader);

				if (!loginAndRegistService.ReaderLogin(reader)) {
						msg.setFlag(false);
						msg.setMessage("用户名或密码错误！请检查后重试！");
						session.setAttribute("user_reader", null);
				} else {
						Reader readerLogin = loginAndRegistService.getReaderMsgByName(reader.getNickName());
						msg.setFlag(true);
						msg.setMessage("登录成功！");
						session.setAttribute("user_reader", readerLogin);
						msg.setMessage(referUrl);
				}
				log.info("readerlogin:" + msg);
				log.info("session user:" + session.getAttribute("user_reader"));
				return msg;
		}

		@ResponseBody
		@RequestMapping("authregist")
		public Msg authRegist(Author author, HttpSession session,HttpServletRequest request) {
				Msg msg = new Msg(true);
				String password = request.getParameter("password");
				String nickName = request.getParameter("nickName");
				String confirmPassword = request.getParameter("confirmPassword");
				Author rd = loginAndRegistService.getAuthMsgByName(nickName);
				if (rd!=null){
						msg.setFlag(false);
						msg.setMessage("注册失败！用户名以存在！");
						return msg;
				}else if(!password.equals(confirmPassword)){
						msg.setFlag(false);
						msg.setMessage("两次输入密码不一致！");
						return msg;
				}


				if (loginAndRegistService.AuthRegist(author)) {
						msg.setFlag(true);
						msg.setMessage("注册成功！");
						session.removeAttribute("user_reader");
						session.setAttribute("user_auth", author);
				} else {
						msg.setFlag(false);
						msg.setMessage("注册失败！用户名以存在！");
				}
				log.info("authregist:" + msg);
				return msg;
		}

		@ResponseBody
		@RequestMapping("readregist")
		public Msg readRegist(Reader reader, HttpSession session,HttpServletRequest request) {
				Msg msg = new Msg(true);
				String password = request.getParameter("password");
				String nickName = request.getParameter("nickName");
				String confirmPassword = request.getParameter("confirmPassword");
				Reader rd = loginAndRegistService.getReaderMsgByName(nickName);
				if (rd!=null){
						msg.setFlag(false);
						msg.setMessage("注册失败！用户名以存在！");
						return msg;
				}else if(!password.equals(confirmPassword)){
						msg.setFlag(false);
						msg.setMessage("两次输入密码不一致！");
						return msg;
				}


				if (loginAndRegistService.ReaderRegist(reader)) {
						msg.setFlag(true);
						msg.setMessage("注册成功！");
						session.removeAttribute("user_auth");
						session.setAttribute("user_reader", reader);
				} else {
						msg.setFlag(false);
						msg.setMessage("注册失败！用户名以存在！");
				}
				log.info("readerregist:" + msg);
				return msg;
		}

		@RequestMapping("unlogin")
		public String unLogin() {
				return "/static/unlogin";
		}
}
